﻿using Acme.SmartWMS.Inbound;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.SmartWMS.Outbound.DispatchDetailService
{
    public class DispatchDetailsAppService:ApplicationService,IDispatchDetailsAppService
    {
        /// <summary>
        /// 出库明细仓储
        /// </summary>
        private readonly IRepository<DispatchDetails,int> _dispatchDetailsRepository;
        public DispatchDetailsAppService(IRepository<DispatchDetails, int> dispatchDetailsRepository)
        {
            _dispatchDetailsRepository = dispatchDetailsRepository;
        }
        /// <summary>
        /// 获取出库明细显示 查询 分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="productCode"></param>
        /// <param name="productName"></param>
        /// <returns></returns>
        public async Task<PagedResult> GetDispatchDetailList(int pageIndex, int pageSize, string? productCode, string? productName)
        {
            var predicate = PredicateBuilder.New<DispatchDetails>(true);
            if (!string.IsNullOrEmpty(productCode))
            {
                predicate = predicate.And(x => x.ProductCode.Contains(productCode));
            }
            if (!string.IsNullOrEmpty(productName))
            {
                predicate = predicate.And(x => x.ProductName.Contains(productName));
            }
            var dto = await _dispatchDetailsRepository.GetListAsync(predicate);
            PagedResult result = new PagedResult();
            result.PageCount = (int)Math.Ceiling((decimal)(dto.Count == 0 ? 1 : dto.Count) / pageSize);
            result.CurrentPage = pageIndex;
            result.PageSize = pageSize;
            result.RowCount = dto.Count;
            result.Queryable = dto.Skip((pageIndex - 1) * pageSize).Take(pageSize).AsQueryable();
            return result;
        }
    }
}
